<?php

namespace App\Common\Models\Db1;

use Illuminate\Database\Eloquent\Model;
use DB;

class DbBase extends Model
{
    public function exec_sql($sql) {
        $sql = trim($sql);
        if (stripos($sql, 'update') == 0) {
            return DB::connection('mysql')->update($sql);
        }
        if (stripos($sql, 'delete') == 0) {
            return DB::connection('mysql')->delete($sql);
        }
        if (stripos($sql, 'insert') == 0) {
            return DB::connection('mysql')->insert($sql);
        }
        return false;
    }

    public function exec($sql, array $bind=null, $accos = '') {
        return $this->exec_sql($sql);
    }

	public function findOne($where) {
        $model = $this;
		if (!is_array($where)) {
			$data = $this->where('id', '=', $where)->get();
		} else {
			foreach ($where as $k => $v) {
				if ($k == '$lte') {
					foreach ($v as $k2 => $v2) {
						$model = $model->where($k2, '<=', $v2);
					}
					continue;
				}
				if ($k == '$gte') {
					foreach ($v as $k2 => $v2) {
						$model = $model->where($k2, '>=', $v2);
					}
					continue;
				}
				if ($k == '$ne') {
					foreach ($v as $k2 => $v2) {
						$model = $model->where($k2, '!=', $v2);
					}
					continue;
				}
				if ($k == '$in') {
					foreach ($v as $k2 => $v2) {
						$model = $model->whereIn($k2, $v2);
					}
					continue;
				}

				$model = $model->where($k, '=', $v);
			}

			$data = $model->get();
		}
		if ($data && $data->toArray()) {
			$row = $data[0];
		} else {
			$row = array();
		}
		return $row;
	}

    public function findAll($where, $start = 0, $size = 100, $sortKey = '') {
        $data = [];
        $model = $this;
        foreach ($where as $k => $v) {
            if ($k == '$lte') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '<=', $v2);
                }
                continue;
            }
            if ($k == '$gte') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '>=', $v2);
                }
                continue;
            }
            if ($k == '$ne') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '!=', $v2);
                }
                continue;
            }
            if ($k == '$in') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->whereIn($k2, $v2);
                }
                continue;
            }

            $model = $model->where($k, '=', $v);
        }

        if ($sortKey) {
            $list1 = explode(' ', $sortKey);
            $model = $model->orderBy($list1[0], $list1[1]);
        }
        $data = $model->offset($start)->limit($size)->get();
        return $data;
    }

    public function getCount($where) {
        $data = [];
        $model = $this;
        foreach ($where as $k => $v) {
            if ($k == '$lte') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '<=', $v2);
                }
                continue;
            }
            if ($k == '$lt') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '<', $v2);
                }
                continue;
            }
            if ($k == '$gte') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '>=', $v2);
                }
                continue;
            }
            if ($k == '$gt') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '>', $v2);
                }
                continue;
            }
            if ($k == '$ne') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->where($k2, '!=', $v2);
                }
                continue;
            }
            if ($k == '$in') {
                foreach ($v as $k2 => $v2) {
                    $model = $model->whereIn($k2, $v2);
                }
                continue;
            }

            $model = $model->where($k, '=', $v);
        }
        return $model->count();
    }

    public function getErrorMessages($type=null) {
        $msg = array();
        /*
        foreach($this->getMessages() as $message) {
            if(is_null($type) || ($message->getType() === $type)) {
                $msg[] = $message->getMessage();
            }
        }
         */

        return join(PHP_EOL, $msg);
    }
}
